#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Extract all needed files       #
#        from PVCS Manager            #
#                                     #
#  GDE 06/02/2004 v2.0                #
#       Add option to get files       #
#       with all subdirectories       #
#                                     #
#  GDE 03/07/2003 v1.4                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 13/01/2003 v1.3                #
#       add MAKEFILE=get_pvcs.mak     #
#                                     #
#  GDE 07/05/2002 v1.2                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 17/04/2002 v1.1                #
#       update the version            #
#        options, and                 #
#        help sections                #
#                                     #
#  GDE 20/12/2001 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: specific option                 #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=2.0
export SCRIPT_SGT_DATE=06/03/2004
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=get_pvcs
export SCRIPT_SGT_USAGE="Extract all needed files from PVCS Manager"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



# Default value :
export OPTION_MAK=

#-------------------------------------#
# Parameters section
#-------------------------------------#
#---------------------
# Parameter 2 -> get with all subdirectories
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ $2 = -d ]
	then
		export OPTION_MAK=GET_SUBDIRECTORY=ON
	else
		export MSG_ERROR_SCRIPT=2
		export CMD_SCRIPT=Error
	fi
fi


#---------------------
# Parameter 1 -> target file / specific option
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					else
						if [ $1 = -rte ]
						then
							export CMD_OPTION=rte
						else
							if [ $1 = -sgt ]
							then
								export CMD_OPTION=sgt
							else
								export MSG_ERROR_SCRIPT=1
								export CMD_SCRIPT=Error
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Get files from PVCS Manager and make
# the output directory to lower case
#---------------------
if [ -z $CMD_SCRIPT ] && [ ! -z $CMD_OPTION ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	
	# Get RTE Kernel
	if [ $CMD_OPTION = rte ]
	then
		if [ -d ../rte ]
		then
			mkdir -p -m775 ../rte
		fi
		make -f ../mak/gen.mak TARGET=rte_pvcs MAKEFILE=get_pvcs.mak GET_KERNEL=ON
		make -f ../mak/gen.mak TARGET=rte_pvcs MAKEFILE=get_pvcs.mak
		exit
	fi

	# Get SGT resources
	if [ $CMD_OPTION = sgt ]
	then
		make -f ../mak/gen.mak TARGET=pvcs MAKEFILE=get_pvcs.mak $OPTION_MAK
		exit
	fi
fi


#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi


#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [[BASIC_OPTIONS] | [ADVANCED_OPTIONS]] "
	echo "  By default all files are extract on a single directory"
	echo " "
	echo "  [BASIC_OPTIONS]"
	echo " "
	echo "  -rte Get RTE resources from PVCS"
	echo "  -sgt Get target resources from PVCS"
	echo " "
	echo "  [ADVANCED_OPTIONS]"
	echo "  Use only after -sgt"
	echo "  -d Get all files with all subdirectories"
	echo " "
	echo "-------------------------------------"
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $1 is an unknown option !"
	fi
	if [ $MSG_ERROR_SCRIPT = 2 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $2 is an unknown option !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi